#ifndef __SEARCHENGINE__H_
#define __SEARCHENGINE__H_

#include <map>
#include <string>
#include "../../catalog/Catalogues.h"
#include "regex.h"
#include "../server/thread/Thread.h"
#include "../../catalog/audio/AudioCatalog.h"
#include "../../catalog/source/CodeCatalog.h"
#include "../../catalog/document/DocumentCatalog.h"
#include "../indexer/FileSystemIndexer.h"
#include <exception>
#include "../../catalog/photo/PhotoCatalog.h"

class ServerSocketImpl;

typedef std::string(SearchEngine::*Method)(std::string);

typedef std::map<std::string,Method> MapMethods;

typedef std::pair<std::string,std::vector<std::string> > Return;

typedef std::pair<std::vector<std::string> ,std::vector<std::pair<unsigned,char> > > RankPair;

class SearchEngine {

 public:
    SearchEngine(int port=9898);

    virtual void index();

    virtual void reindex();

    virtual void processEvent();


    Method getCommand(std::string) throw (std::exception);

    std::string stopServer(std::string="");

    std::string rankQuery(std::string);

    std::string rankQueryCatalog(std::string);

    std::string wildCardQuery(std::string);

    std::string wildCardQueryCatalogue(std::string);

    std::string listDirectories(std::string dir="");

    std::string addDirectory(std::string);

    std::string removeDirectory(std::string);

    std::string reindex(std::string par="");

    void agregarRuta(std::string ruta);

    Catalogues* getCatalogue(std::string key);

    void initPaths();

    void writePaths();

    ~SearchEngine();

 private:
    Thread thread;
    FileSystemIndexer indexer;
    std::set<std::string> paths;
    std::map<std::string,Catalogues*> catalogues;
    std::map<std::string,Catalogues*> reverseCat;
    MapMethods mapMethods;
    void initMap();
    void initMapMethods();
    RankPair parseArgs(std::string);
    void buildResponse(std::vector<std::string>,std::string,std::string&);
};

#include "../server/ServerSocketImpl.h"

#endif // SearchEngine_h

